import prj002
import prj003
import prj004
import prj005

from celery_tasks.celery import app

from utils.saveexcel import create_excel
from django.utils import timezone
from datetime import datetime


@app.task(name='celery_save_excel')
def celery_save_excel(excel_data, user_email, serial=None, project_name=None):
    """ 未提交 => 已提交 [状态改变时触发]
    生成文件
    """
    total_data = []

    total_data.append(excel_data)

    create_excel(total_data, user_email, project_name=project_name, types='celery_status', serial=serial)


@app.task(name='celery_save_data')
def celery_save_data(before_fields, after_fields, serial, project_name=None):
    current_time = datetime.now(tz=timezone.utc)

    if project_name == 'prj002':
        _model = prj002.models.status.InfoStatus
        turn_to_chinese = prj002.utils.constants.turn_to_chinese
    elif project_name == 'prj003':
        _model = prj003.models.status.InfoStatus
        turn_to_chinese = prj003.utils.constants.turn_to_chinese003
    elif project_name == 'prj004':
        _model = prj004.models.status.InfoStatus
        turn_to_chinese = prj004.utils.constants.turn_to_chinese004
    elif project_name == 'prj005':
        _model = prj005.models.status.InfoStatus
        turn_to_chinese = prj005.utils.constants.turn_to_chinese005

    for key, after_value in after_fields.items():
        before_value = before_fields.get(key)
        if not before_value and not after_value:
            continue

        if not before_value == after_value:
            model_object = _model()
            model_object.info_serial = serial
            # model_object.info_id = info_id
            model_object.fix_name = key
            model_object.fix_name_chinese = turn_to_chinese.get(key)
            model_object.fix_before = before_value
            model_object.fix_after = after_value
            model_object.create_time = current_time
            model_object.save()
